<?php
class Admin extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->Model(array("Madmin","Mlevel"));
	}
	public function index(){
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_admin'),base_url().'admincp/admin');
		if($this->input->is_ajax_request()){
            $output = $this->Madmin->listAdmin();
            $this->output->set_output(json_encode($output));
        }else{
            $data['template']="admin/view";
            $data['title_page']='<i class="icon20 i-user-7"></i> '.trans('title_admin');
			$this->load->view("admincp/template",$data);
        }
	}

	public function add(){
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_admin'),base_url().'admincp/admin');
		$this->breadcrumb->append_crumb(trans('breadcrumb_admin_add'),base_url().'admincp/admin/add');
		$data['template']="admin/add";
		$data['title_page']='<i class="icon20 i-plus-circle-2"></i> '.trans('title_admin_add');
		$data['level']=$this->Mlevel->list_all();
		$path_upload = 'public/images/admin/avatar';
		$config['upload_path'] = './'.$path_upload;
		$config['allowed_types'] = 'gif|jpg|png';
		$config['encrypt_name'] = TRUE;
		$this->load->library('upload', $config);
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$img = $this->upload->data();
			$image = $img['file_name'];
			$this->load->library('image_lib'); 
			$config['image_library'] = 'gd2';
			$config['source_image']	= './'.$path_upload.$image;
			$config['create_thumb'] = FALSE;
			$config['maintain_ratio'] = TRUE;
			$config['width']	 = 150;
			$config['height']	= 150;
			$this->image_lib->initialize($config);
			$this->image_lib->resize();
			$data_insert = array(
				'admin_fullname' => $this->input->post('admin_fullname'),
				'admin_email' => $this->input->post('admin_email'),
				'admin_phone' => $this->input->post('admin_phone'),
				'admin_name' => $this->input->post('admin_name'),
				'password' => md5(md5($this->input->post('password'))),
				'level_id' => $this->input->post('level'),
				'date_add' => date('Y-m-d',time()), 
			);
			$this->Madmin->insert($data_insert);
			$this->session->set_flashdata(array("success"=>trans('message_create_successfully')));
			redirect(base_url()."admincp/admin");
		}
		$this->load->view("admincp/template",$data);
	}

	public function edit(){
		$id = $this->uri->segment(5);
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_admin'),base_url().'admincp/admin');
		$this->breadcrumb->append_crumb(trans('breadcrumb_admin_edit'),base_url().'admincp/admin/edit/id/'.$id);
		$data['template']="admin/edit";
		$data['title_page']='<i class="icon20 i-pen-2"></i> '.trans('title_admin_edit');
		$data['level']=$this->Mlevel->list_all();
		$data['info']=$this->Madmin->get_data($id);
		$path_upload = 'public/images/admin/avatar';
		$config['upload_path'] = './'.$path_upload;
		$config['allowed_types'] = 'gif|jpg|png';
		$config['encrypt_name'] = TRUE;
		$this->load->library('upload', $config);
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$img = $this->upload->data();
			$image = $img['file_name'];
			$this->load->library('image_lib'); 
			$config['image_library'] = 'gd2';
			$config['source_image']	= './'.$path_upload.$image;
			$config['create_thumb'] = FALSE;
			$config['maintain_ratio'] = TRUE;
			$config['width']	 = 150;
			$config['height']	= 150;
			$this->image_lib->initialize($config);
			$this->image_lib->resize();
			$password = $this->input->post('password');
			if(isValidMd5($password) != 1){
				$password = md5(md5($password));	
			}
			$data_update = array(
				'admin_fullname' => $this->input->post('admin_fullname'),
				'admin_email' => $this->input->post('admin_email'),
				'admin_phone' => $this->input->post('admin_phone'),
				'password' => $password,
				'level_id' => $this->input->post('level'),
				'date_edit' => date('Y-m-d',time()), 
			);
			$this->Madmin->update($data_update,$id);
			$this->session->set_flashdata(array("success"=>trans('message_edit_successfully')));
			redirect(base_url()."admincp/admin");
		}
		$this->load->view("admincp/template",$data);
	}

	public function profile(){
		$data['template']="admin/profile";
		$id = $this->session->userdata['ses_adminid'];
		$this->load->view("admincp/template",$data);
	}

	public function delete(){
		$id = $this->uri->segment(5);
		$this->Madmin->delete($id);
		$this->session->set_flashdata(array("success"=>trans('message_delete_successfully')));
		redirect(base_url()."admincp/admin");
	}

	public function check_username($name,$admin_id){
		if($this->Madmin->check_name($name, $admin_id) == FALSE){
			return FALSE;
		}else{
			return TRUE;
		}
	}
}